package com.example.kt_fast.ui

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.view.View
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.kt_common.base.BaseActivity
import com.example.kt_common.network.BaseStateObserver
import com.example.kt_common.util.Constants
import com.example.kt_common.util.ToastUtil
import com.example.kt_fast.R
import com.example.kt_fast.databinding.ActivityAddDriverBinding
import com.example.kt_fast.bean.resp.UploadImgBean
import com.example.kt_fast.viewmodel.FastViewModel
import com.github.dhaval2404.imagepicker.ImagePicker
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import org.koin.androidx.viewmodel.ext.android.viewModel
import java.io.File

@Route(path = Constants.PATH_ADD_DRIVER)
class AddDriverActivity : BaseActivity<ActivityAddDriverBinding>(), View.OnClickListener {
    private val addDriverViewModel: FastViewModel by viewModel()

    private lateinit var file: File
    private lateinit var activityLauncher: ActivityResultLauncher<Intent>
    override fun getLayoutID(): Int {
        return R.layout.activity_add_driver
    }

    override fun init() {
        activityLauncher = registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) { result: ActivityResult ->
            if (result.resultCode == Activity.RESULT_OK) {
                val uri = result.data?.data
                uri?.let {
//                    loadImageFromUri(it)
                    file = File(uri.path) // 直接从URI获取路径创建File对象
                    //服务器请求 上传图片
                    var requestBody = RequestBody.create("multipart/form-data".toMediaType(), file)
                    var part =
                        MultipartBody.Part.createFormData("file", file.getName(), requestBody)
                    addDriverViewModel.uploadImgBean(part)
                }
            }
        }
        mBind.ivDriver.setOnClickListener(this)
        addDriverViewModel.uploadImgData.observe(
            this,
            object : BaseStateObserver<UploadImgBean>(null) {
                override fun getRespDataSuccess(it: UploadImgBean) {
                    ToastUtil.showMsg("图片上传成功")
                }
            })
        addDriverViewModel.addDriverInfoData.observe(
            this,
            object : BaseStateObserver<String>(null) {
                override fun getRespDataSuccess(it: String) {

                }
            })


    }


    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.iv_driver -> checkPermissions()
        }
    }

    // 显示选择弹窗
    private fun showPickerDialog() {
        ImagePicker.with(this).crop() // 启用裁剪
            .compress(1024) // 压缩图片
            .maxResultSize(1080, 1080) // 限制分辨率
            .createIntent { intent ->
                activityLauncher.launch(intent)
            }
    }

    // 检查并请求权限
    private fun checkPermissions() {
        val requiredPermissions = mutableListOf<String>()
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
            requiredPermissions.add(Manifest.permission.READ_EXTERNAL_STORAGE)
        }
        requiredPermissions.add(Manifest.permission.CAMERA)

        val hasAllPermissions = requiredPermissions.all {
            ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED
        }

        if (!hasAllPermissions) {
//           PermissionLauncher.launch(requiredPermissions.toTypedArray())
            ToastUtil.showMsg("没有权限")
        } else {
            showPickerDialog()
        }
    }

}